import { useEffect, useState } from 'react';
import { getClientH } from '../base/util';

const get_h = (mhigh, minh) => {
    if (!minh) {
        return 0;
    }

    let h = getClientH() - mhigh;
    if (h < minh) {
        h = minh;
    }

    return h;
};

const useHigh = (mhigh, minh) => {
    const [height, setHigh] = useState(get_h(mhigh, minh));

    useEffect(() => {
        const onResize = () => {
            if (minh) {
                setHigh(get_h(mhigh, minh));
            }
        }

        window.addEventListener('resize', onResize)

        return () => window.removeEventListener('resize', onResize);
    }, [minh, mhigh]);

    return [height];
};

export {useHigh};
